﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 职员信息
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.12.01 
    /// </summary>
    internal class EmployeeService : DataFactory, EmployeeIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<EmployeeEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<EmployeeEntity>().OrderByDescending(o => o.CreatorTime).ToListAsync();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<EmployeeEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                var whereLambda = ExtLinq.True<EmployeeEntity>();
                var queryParam = input.queryJson.ToObject();
                //查询条件
                if (!queryParam["condition"].IsEmpty() && !input.keyword.IsEmpty())
                {
                    string propertyName = queryParam["condition"].ToString();
                    string propertyValue = input.keyword;
                    switch (propertyName)
                    {
                        case "EnCode":            //工号
                            whereLambda = whereLambda.And(t => t.EnCode.Contains(propertyValue));
                            break;
                        case "FullName":          //姓名
                            whereLambda = whereLambda.And(t => t.FullName.Contains(propertyValue));
                            break;
                        case "Telephone":         //电话
                            whereLambda = whereLambda.And(t => t.Telephone.Contains(propertyValue));
                            break;
                        case "DepartmentName":    //部门
                            whereLambda = whereLambda.And(t => t.DepartmentName.Contains(propertyValue));
                            break;
                        case "PositionName":      //职位
                            whereLambda = whereLambda.And(t => t.PositionName.Contains(propertyValue));
                            break;
                        default:
                            break;
                    }
                }

                return await db.GetIQueryable<EmployeeEntity>().Where(whereLambda).OrderByDescending(t => t.CreatorTime).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<EmployeeEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<EmployeeEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Delete(EmployeeEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entitys">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(List<EmployeeEntity> entitys)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entitys);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(EmployeeEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(EmployeeEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
    }
}
